package com.osfw.dbadminer.service.config.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.osfw.dbadminer.entity.config.DbaConfigProcess;
import com.osfw.dbadminer.entity.config.DbaConfigScope;
import com.osfw.dbadminer.mapper.config.DbaConfigScopeMapper;
import com.osfw.dbadminer.service.config.IDbaConfigScopeService;
import com.osfw.framework.constants.Constants;
import com.osfw.framework.utils.StringUtils;
import com.osfw.system.entity.manage.SysUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据库操作权限范围配置 服务层处理
 */
@Service
public class DbaConfigScopeServiceImpl extends ServiceImpl<DbaConfigScopeMapper, DbaConfigScope> implements IDbaConfigScopeService {

    @Override
    public List<DbaConfigScope> selectScopeList(DbaConfigScope scope) {
        LambdaQueryWrapper<DbaConfigScope> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DbaConfigScope::getConnectId, scope.getConnectId() == null ? 1l : scope.getConnectId());
        queryWrapper.eq(StringUtils.isNotEmpty(scope.getDatabaseName()), DbaConfigScope::getDatabaseName, scope.getDatabaseName());
        queryWrapper.eq(StringUtils.isNotEmpty(scope.getDatatableName()), DbaConfigScope::getDatatableName, scope.getDatatableName());
        return list(queryWrapper);
    }

    @Override
    @Transactional
    public boolean authUsers(Long connectId, String databaseName, String datatableName, String userIds) {
        if (StringUtils.isNotEmpty(userIds)) {
            List<DbaConfigScope> list = Arrays.stream(userIds.split(",")).map(userId -> {
                DbaConfigScope scope = new DbaConfigScope();
                scope.setConnectId(connectId);
                scope.setDatabaseName(databaseName);
                scope.setDatatableName(datatableName);
                scope.setUserId(Long.getLong(userId));
                scope.setQueryPermit(Constants.YES_INTEGER);
                scope.setQueryApproval(Constants.NO_INTEGER);
                scope.setAddPermit(Constants.NO_INTEGER);
                scope.setAddApproval(Constants.NO_INTEGER);
                scope.setModifyPermit(Constants.NO_INTEGER);
                scope.setModifyApproval(Constants.NO_INTEGER);
                scope.setDeletePermit(Constants.NO_INTEGER);
                scope.setDeleteApproval(Constants.NO_INTEGER);
                scope.setImportPermit(Constants.NO_INTEGER);
                scope.setImportApproval(Constants.NO_INTEGER);
                scope.setExportPermit(Constants.YES_INTEGER);
                scope.setExportApproval(Constants.NO_INTEGER);
                scope.setOtherPermit(Constants.NO_INTEGER);
                scope.setOtherApproval(Constants.NO_INTEGER);
                return scope;
            }).collect(Collectors.toList());
        }
        return false;
    }

    @Override
    public List<Long> selectConnectIdsByUser(SysUser sysUser) {
        List<DbaConfigScope> list = new ArrayList<>();
        if (sysUser.isAdmin()) {
            list = list();
        } else {
            LambdaQueryWrapper<DbaConfigScope> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select(DbaConfigScope::getConnectId);
            queryWrapper.eq(DbaConfigScope::getUserId, sysUser.getUserId());
            list = list(queryWrapper);
        }
        return list.stream().filter(x -> x != null && x.getConnectId() != null).map(x -> x.getConnectId()).distinct().collect(Collectors.toList());
    }

    @Override
    public List<String> selectDatabaseNamesByConnectAndUser(Long connectId, SysUser sysUser) {
        List<DbaConfigScope> list = new ArrayList<>();
        if (sysUser.isAdmin()) {
            LambdaQueryWrapper<DbaConfigScope> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select(DbaConfigScope::getDatabaseName);
            queryWrapper.eq(DbaConfigScope::getConnectId, connectId);
            queryWrapper.isNotNull(DbaConfigScope::getDatabaseName);
            list = list(queryWrapper);
        } else {
            LambdaQueryWrapper<DbaConfigScope> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select(DbaConfigScope::getDatabaseName);
            queryWrapper.eq(DbaConfigScope::getConnectId, connectId);
            queryWrapper.eq(DbaConfigScope::getUserId, sysUser.getUserId());
            queryWrapper.isNotNull(DbaConfigScope::getDatabaseName);
            list = list(queryWrapper);
        }
        return list.stream().map(x -> x.getDatabaseName()).distinct().collect(Collectors.toList());
    }

}
